<?php

/**

 * @authors alex (20779512@qq.com)

 * @date    2014-03-24 17:25:39

 * @version 1.0

 */



namespace Admin\Controller;



class GroupController extends AdminController {

    

    #用户组列表

    public function index(){ 

        $obj = M('role');

        $list = $obj->select();

        $this->assign('list', $list);       

    	$this->display();

    }



    #添加用户组

    public function add(){

    	$this->display();

    }



    #保存用户组

    public function save(){



        $obj = D('Role');



        if ($date = $obj->create()) {

            if ($obj->add($date)) {

                $state = 'SUCCESS';

                $message = 'Successful operation';

                $url = U('Group/index');

            } else {

                $state = 'Operation failed';

            }

        } else {

            $state = $obj->getError();

        }

        echo $this->echo_json_str($state,$message,$url);

    }



    #编辑用户组

    public function edit(){



        $id = (int) I('get.id');

        if (!isset ($id)) {

            $this->error('Parameter error');

        }

        $obj = M('Role');

        $list = $obj->where('id=' . $id)->find();

        $this->assign('list', $list);

        $this->display();

    }



    #更新用户组

    public function update(){



        $obj = D('Role');



        if ($date = $obj->create()) {

            if ($obj->save($date)) {

                $state = 'SUCCESS';

                $message = 'Successful operation';

                $url = U('Group/index');

            } else {

                $state = 'Operation failed';

            }

        } else {

            $state = $obj->getError();

        }

        echo $this->echo_json_str($state,$message,$url);

    }



    //删除组

    public function del() {

        $id = $_GET['id'];

        if (!isset ($id)) {

            $this->error("You do not select any information!");

        }



        $obj = M('Role');

        $list = $obj->where('id=' . $id)->delete();

        

        $access = M('access');

        $access->where('role_id=' . $id)->delete();



        if(!$list){

            echo '0';

        }else{

            echo '1';

        }

    }



    #用户组权限管理

    public function competence() {

        $id = $_GET['id'];

        if (!isset ($id)) {

            $this->error("Parameter error!");

        }

        

        $obj = M('Role');

        $list = $obj->field('name')->where('id='.$id)->find();

        $this->assign('name',$list['name']);

        

        $obj = M('access');

        $node = $obj->field('node_id')->where('role_id='.$id)->select();

        

        for($i=0; $i<count($node); $i++){

            $nodeArr[$i]=$node[$i]['node_id'];      

        }

                

        $obj = M('node');

        $list = $obj->where('level=2')->select();



        foreach ($list as $key => $value) {

            $Controller = $obj->where('pid='.$value['id'])->select();

            $list[$key]['child'] = $Controller;

        }		

		

        $this->assign('nodeArr',$nodeArr);

        $this->assign('id',$id);

        $this->assign('list',$list);

        $this->display();

    }



    #保存用户组权限

    public function access(){       

        $mod = I('post.mod');

        $role_id = I('post.role_id');

        

        if(!isset($role_id)||$role_id==''){

            $this->error("Parameter error!");

        }

        

        if(!isset($mod)||$mod==''){

            $this->error("You do not select any information!");

        }

        

        $obj = M('access');

        $obj->where('role_id='.$role_id)->delete();

        

        $data['role_id'] = $role_id;        

        $data['node_id'] = 1;   

        $data['level'] = 1;

        $data['pid'] = 0;

        $obj->add($data);

        

        foreach ($mod as $v){

            $arr = explode("-",$v);         

            $data['role_id'] = $role_id;

            $data['node_id'] = $arr[0];

            $data['level'] = $arr[1];

            $data['pid'] = $arr[2];

            $obj->add($data);

        }

        

        $this->success('Successful operation');

        

    }

}